package org.turnlink.sclm.dao.hibernate;

import java.util.List;

import org.hibernate.SessionFactory;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import org.turnlink.sclm.dao.ContactDao;
import org.turnlink.sclm.model.Contact;

//This will make it easier to be autowired
@Repository("contactDao")
// Default is read only
@Transactional
public class ContactDaoHibernate implements ContactDao  {

	private HibernateTemplate hibernateTemplate;

	@Autowired(required=true)
	public void setSessionFactory(SessionFactory sessionFactory) {
		hibernateTemplate = new HibernateTemplate(sessionFactory);
	}

	/*
	 * (non-Javadoc)
	 * @see org.turnlink.sclm.dao.ContactDao#findById(java.lang.Integer)
	 */
	public Contact findById(Integer contactId) {
		return hibernateTemplate.get(Contact.class,contactId);
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.turnlink.sclm.dao.ContactDao#findByDetatchedCriteria(org.hibernate.criterion.DetachedCriteria)
	 */
	@SuppressWarnings("unchecked")
	public List<Contact> findByDetatchedCriteria(DetachedCriteria criteria) {
		return hibernateTemplate.findByCriteria(criteria);
	}

	/*
	 * (non-Javadoc)
	 * @see org.turnlink.sclm.dao.ContactDao#saveOrUpdate(java.lang.Integer, org.turnlink.sclm.model.Contact)
	 */
	@Transactional(readOnly = false)
	public void saveOrUpdate(Contact contact) {
		hibernateTemplate.saveOrUpdate(contact);
	}

	/*
	 * (non-Javadoc)
	 * @see org.turnlink.sclm.dao.ContactDao#delete(org.turnlink.sclm.model.Contact)
	 */
	@Transactional(readOnly = false)
	public void remove(Contact contact) {
		hibernateTemplate.delete(contact);
	}
}
